package com.lovo.sheyingback.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.sheyingback.bean.Content;
import com.lovo.sheyingback.bean.Swiper;
import com.lovo.sheyingback.bean.Type;
import com.lovo.sheyingback.service.SwiperService;
import com.lovo.sheyingback.dao.SwiperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author BDK-THINKPAD
* @description 针对表【t_swiper(首页轮播图表)】的数据库操作Service实现
* @createDate 2023-07-28 15:47:06
*/
@Service
public class SwiperServiceImpl implements SwiperService{

    @Autowired
    private SwiperMapper swiperMapper;
    @Override
    public PageInfo<Swiper> listSwiper(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
        List<Swiper> swipers = swiperMapper.listSwiper();
        return new PageInfo<>(swipers);
    }

    @Override
    @Transactional
    public int insertMore(Integer [] contentIds) {
        int maxIndex = swiperMapper.getMaxIndex();
        for(int i = 0;i < contentIds.length;i++){
            Swiper swiper = new Swiper();
            swiper.setIndex(maxIndex + i);
            swiper.setContent(new Content(contentIds[i]));
            swiper.setCreateDate(LocalDateTime.now());
            swiper.setUpdateDate(LocalDateTime.now());
            swiperMapper.insert(swiper);
        }
        return 1;
    }

    @Override
    @Transactional
    public int updateIndex(Integer id, Integer val) {
        Swiper swiper = swiperMapper.getById(id);
        swiper.setIndex(swiper.getIndex() +val);
        swiperMapper.updateIndex(swiper.getIndex(),-val);
        swiperMapper.update(swiper);
        return 1;
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        swiperMapper.updateAfterIndex(id);
        return swiperMapper.delete(id);

    }
}




